﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using BlogMVC.ModelEntity;


namespace BlogMVC.Areas.Admin.Controllers
{
    public class CategoryController : Controller
    {
        DataBaseDataContext content = new DataBaseDataContext();

        // GET: /Admin/Category/

        public ActionResult Index()
        {
            var categorys = content.Categories.ToList();
            return View(categorys);
        }
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(Category category)
        {
            try
            {
                content.Categories.InsertOnSubmit(category);
                content.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public ActionResult Delete(int id)
        {
            Category c = content.Categories.Where(o => o.CategoryId == id).Single();
            return View(c);
        }
        [HttpPost]
        public ActionResult Delete(int id,Category category)
        {
            try
            {
                category = content.Categories
                    .Where(o => o.CategoryId == id)
                        .Single();
                content.Categories.DeleteOnSubmit(category);
                content.SubmitChanges();
                return RedirectToAction("Index");

            }
            catch (Exception)
            {
                
                throw;
          }

        }
        public ActionResult Edit(int id)
        {
            Category a = content.Categories.Where(o => o.CategoryId == id).Single();
            return View(a);
        }
        [HttpPost]
        public ActionResult Edit(int id, Category category)
        {
            try
            {
                 category = content.Categories.Where(o => o.CategoryId == id).Single();
                if (category!=null)
                    TryUpdateModel(category);
                    content.SubmitChanges();
                     return RedirectToAction("Index");
            }
            catch (Exception)
            {
                return RedirectToAction("Index");
            }
        }
        public ActionResult Details(int id)
        {
            try
            {
                return View(content.Categories.Where(o => o.CategoryId == id).Single());
            }
            catch (Exception)
            {
                
                throw;
            }
        }

    }
}
